package multidraw.dialog;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import multidraw.model.DrawingCanvasModel;
import multidraw.tools.ToolInfo;

public class ShareToolDialog extends JDialog {
	private JList availableToolList;
	
	private DefaultListModel listModel;
	
	
	public ShareToolDialog(JFrame owner, DrawingCanvasModel model) {
		//Construct a panel with a list of avaible tool to share
		super(owner, "Share Tool Dialog", true);
		
		JPanel panel = new JPanel();
		
		setSize(300, 400);
		
		panel.setLayout(new BorderLayout());
		
		listModel = new DefaultListModel();
		for (ToolInfo tool: model.getAllTools()) {
			if (!model.getSessionToolList().contains(tool)) {
				listModel.addElement(tool);
			}
		}
		availableToolList = new JList(listModel);
		
		JScrollPane scrollpane = new JScrollPane(availableToolList);

		JLabel label = new JLabel("Available Tools to Share");
		
		JPanel buttonPanel = new JPanel();
		
		buttonPanel.setLayout(new GridLayout(1,2,5,5));
		panel.add(buttonPanel, BorderLayout.SOUTH);
		
		JButton okButton = new JButton("OK");
		buttonPanel.add(okButton);
		
		okButton.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				okButtonClicked();
			}
		});
		
		
		JButton cancelButton = new JButton("Cancel");
		buttonPanel.add(cancelButton);
		
		cancelButton.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				cancelButtonClicked();
			}
		});
		
		panel.add(label, BorderLayout.NORTH);
		panel.add(scrollpane, BorderLayout.CENTER);
		
		setContentPane(panel);
		
	}
	
	private boolean ok;
	
	public boolean showDialog() {
		ok = false;
		
		setVisible(true);
		
		return ok;
	}
	
	public void okButtonClicked() {
		ok = true;
		setVisible(false);
	}
	
	public void cancelButtonClicked() {
		ok = false;
		setVisible(false);
	}
	
	public ArrayList<ToolInfo> getSelectedTools() {
		ArrayList<ToolInfo> result = new ArrayList<ToolInfo>();
		for (Object t: availableToolList.getSelectedValues()) {
			result.add((ToolInfo)t);
		}
		return result;
	}
}
